#include "ustandard/ustandard.h"
int yield_cpu(struct timeval* tv, int usec)
{
    int ret = 0;

    struct timeval cur;
    struct timeval to;

    if(0==tv->tv_sec && 0==tv->tv_usec) {
        utv_get_monotonic(tv);
        return ret;
    }
    else {
        utv_get_monotonic(&cur);
        int64_t usec_count = utv_count(tv, &cur);
        if(usec_count < (int64_t)usec) {
            to.tv_sec = (usec - usec_count)/(1000*1000);
            to.tv_usec = (usec - usec_count)%(1000*1000);
            select(0, NULL, NULL, NULL, &to);
        }
    }

    utv_get_monotonic(tv);
    return ret;
}
int main()
{
    FILE* fp = fopen("/tmp/yield", "w");
    setbuf(fp, NULL);
    struct timeval tv = {0, 0};
    while(1) {
        yield_cpu(&tv, 10000);
        fprintf(fp, "yield_cpu finished(%ld:%06ld).\n", tv.tv_sec, tv.tv_usec);
        usleep(20*1000);
    }
    return 0;
}
